<?php

namespace App\Http\Controllers;

use InfyOm\Generator\Utils\ResponseUtil;
use App\Models\WechatUserSession;
use App\Models\WechatUser;
use Illuminate\Support\Facades\Log;
use Response;

/**
 * @SWG\Swagger(
 *   basePath="/api/v1",
 *   @SWG\Info(
 *     title="Laravel Generator APIs",
 *     version="1.0.0",
 *   )
 * )
 * This class should be parent class for other API controllers
 * Class AppBaseController
 */
class AppBaseController extends Controller
{
    public function sendResponse($result, $message)
    {
        return Response::json($this->output($result, $message));
    }

    public function sendError($error, $code = 400)
    {
        return Response::json($this->error($error, [], $code), $code);
    }

    /**
     * Format json, validate the response
     */
    protected function json_format($data, $ret = 1, $info = '')
    {
        if(empty($data))
        {
            $data = new \stdClass;
        }
        return array(
            'ret' => $ret,
            'data' => $data,
            'info' => $info
        );
    }

    protected function output($data = [], $info = '')
    {
        return $this->json_format($data, 1, $info);
    }

    protected function error($info = '', $data = [], $code = 0)
    {
        $ret = 1;
        if ($code >= 400)
        {
            $ret = 0;
        }
        return $this->json_format($data, $ret, $info);
    }

    protected function checkWeachtMiniProgramLogin($request)
    {
        $sessionId = $request->header('Session-Id') ?: $request->input('session_id');
        if ($sessionId)
        {
            $session = WechatUserSession::where('session_id', $sessionId)->with('hasWechatUser')->first();
            if ($session && $session->updated_at->gt(now()->subHours(config('wechat.session_expire_time'))))
            {
                $info = $session->toArray();
                $GLOBALS['wx_user_session'] = [
                    'session_key' => $info['session_key'],
                    'session_id' => $info['session_id'],
                    'wechat_user_id' => $info['has_wechat_user']['id'],
                    'openid' => $info['has_wechat_user']['openid']
                ];

                log::info('wx_user_session: '.json_encode($GLOBALS['wx_user_session']));
                return $GLOBALS['wx_user_session'];
            }
        }

        throw new \Exception('登陆失效', 401);

        return $this->sendError('登陆失效', 401);
        // return response()->json($this->error('登陆失效', [], 401), 401);
    }

    protected function checkWeachtUserByUnionid($request)
    {
        $unionid = $request->input('unionid');
        if ($unionid)
        {
            $user = WechatUser::where('unionid', $unionid)->first();
            if ($user)
            {
                $GLOBALS['wx_user_session'] = [
                    'wechat_user_id' => $user->id,
                    'openid' => $user->openid
                ];

                return $GLOBALS['wx_user_session'];
            }
        }

        return FALSE;
    }
}